function getSocketUrl(systemId: string): string {
  let env = "dev";
  const baseUrl = window.location.href;
  if (baseUrl.indexOf("u1-fls.fuxiaoli.com") != -1) {
    env = "uat1";
  } else if (baseUrl.indexOf("fls1.fuxiaoli.com") != -1) {
    env = "prod";
  } else if (baseUrl.indexOf("192.168") != -1) {
    env = "dev";
  }

  type WsUrlType = {
    dev: string;
    uat1: string;
    prod: string;
    [key: string]: string;
  };

  const wsUrl: WsUrlType = {
    dev: "wss://go-ws.fuxiaoli.com/ws",
    uat1: "wss://u1-go-ws.fuxiaoli.com/ws",
    prod: "wss://go-ws1.fuxiaoli.com/ws",
  };

  return `${wsUrl[env as keyof typeof wsUrl]}?systemId=${systemId}`;
}

type Options = {
  isReconnect?: boolean;
  reconnectCount?: number;
  systemId: string; // ws后端分组id
  callback?: () => void;
};

export class WebsocketApi {
  private url: string = "";
  private isOpen: boolean;
  private isReconnect: boolean;
  private reconnectCount: number;
  private reconnectInterval: number;
  private reconnectCurrent: number;
  public websocket: WebSocket | null = null;
  private reconnectTimer: ReturnType<typeof setTimeout> | null;

  constructor(options: Options) {
    this.url = getSocketUrl(options.systemId); // ws连接地址
    this.isReconnect = options.isReconnect || true; // 是否支持重连
    this.reconnectCount = options.reconnectCount || 3; // 重连最大次数
    this.reconnectInterval = options.reconnectCount || 3000; // 重连间隔时间
    this.reconnectCurrent = 0; // 当前重连几次
    this.isOpen = false; // 是否连接
    this.websocket = null; // ws实例对象
    this.reconnectTimer = null; // 重连定时器

    if (!("WebSocket" in window)) {
      console.warn("浏览器不支持WebSocket");
      return;
    }

    this.websocket = new WebSocket(this.url);

    this.websocket.onopen = () => {
      console.log("连接成功");
      this.isOpen = true;
      options.callback && options.callback();
    };

    this.websocket.onclose = () => {
      console.log("连接关闭");
      this.isOpen = false;
    };

    this.websocket.onerror = (e) => {
      console.error("WebSocket连接发生错误", e);
      // if (!this.isOpen) {
      //   if (this.isReconnect) {
      //     this.reconnectTimer = setTimeout(() => {
      //       if (this.reconnectCurrent > this.reconnectCount) {
      //         clearTimeout(this.reconnectTimer as ReturnType<typeof setTimeout>)
      //         return
      //       }

      //       this.reconnectCurrent += 1
      //       this.reconnect(this.url)
      //     }, this.reconnectInterval)
      //   }
      // }
    };
  }

  // send(data: any, callback?: () => void): WebSocket | undefined {
  //   if (this.websocket?.readyState === this.websocket!.OPEN) {
  //     this.websocket.send(JSON.stringify(data))

  //     if (callback) {
  //       callback()
  //     }
  //   } else if (this.websocket?.readyState === this.websocket!.CONNECTING) {
  //     setTimeout(() => {
  //       this.send(data, callback)
  //     }, 1000)
  //   } else {
  //     this.init(this.url)
  //     setTimeout(() => {
  //       this.send(data, callback)
  //     }, 1000)
  //   }

  //   return this.websocket!
  // }

  // reconnect(_url?: string): void {
  //   const url = _url ? _url : this.baseUrl
  //   if (this.websocket && this.isOpen) {
  //     this.clear()
  //   }
  //   this.websocket = null
  //   this.init(url)
  // }

  // clear(): void {
  //   this.websocket?.close()
  // }
}
